package com.hskn.hss.module.dataBigscreen.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.constant.CommonConstant;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.alarmcar.entity.AlarmCar;
import com.hskn.hss.module.alarmcar.mapper.AlarmCarMapper;
import com.hskn.hss.module.alarmperson.entity.AlarmPerson;
import com.hskn.hss.module.alarmperson.mapper.AlarmPersonMapper;
import com.hskn.hss.module.alarmsheshi.entity.AlarmSheshi;
import com.hskn.hss.module.alarmsheshi.mapper.AlarmSheshiMapper;
import com.hskn.hss.module.bigscreen.mapper.BigScreenMapper;
import com.hskn.hss.module.carTrack.entity.CarTrack;
import com.hskn.hss.module.carTrack.mapper.CarTrackMapper;
import com.hskn.hss.module.carviolation.entity.CarViolation;
import com.hskn.hss.module.carviolation.mapper.CarViolationMapper;
import com.hskn.hss.module.cases.entity.Cases;
import com.hskn.hss.module.cases.mapper.CasesMapper;
import com.hskn.hss.module.dataBigscreen.service.IDataBigScreenService;
import com.hskn.hss.module.dataBigscreen.vo.AbnormalVo;
import com.hskn.hss.module.dataBigscreen.vo.CasesVo;
import com.hskn.hss.module.dataBigscreen.vo.TrackVo;
import com.hskn.hss.module.dataBigscreen.vo.WorkVo;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.module.elementtotal.mapper.ElementTotalMapper;
import com.hskn.hss.module.elementtotal.service.ElementTotalService;
import com.hskn.hss.module.elementtotal.vo.EmpSearchListVo;
import com.hskn.hss.module.employeePunchCard.entity.EmployeePunchCard;
import com.hskn.hss.module.employeePunchCard.mapper.EmployeePunchCardMapper;
import com.hskn.hss.module.employeeTrack.entity.EmployeeTrack;
import com.hskn.hss.module.employeeTrack.mapper.EmployeeTrackMapper;
import com.hskn.hss.module.equiptype.entity.TlEquiptype;
import com.hskn.hss.module.equiptype.mapper.TlEquiptypeMapper;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.module.jobset.mapper.JobsetMapper;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.mapper.RptEmployeeWorkMapper;
import com.hskn.hss.module.tlcar.entity.TICarResult;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlchannel.entity.TlChannel;
import com.hskn.hss.module.tlchannel.mapper.TlChannelMapper;
import com.hskn.hss.module.tldictlevel.service.ITlEmergentDictLevelService;
import com.hskn.hss.module.tldicttype.mapper.TlEmergentDictTypeMapper;
import com.hskn.hss.module.tlemergentplan.entity.TlEmergentPlan;
import com.hskn.hss.module.tlemergentplan.mapper.TlEmergentPlanMapper;
import com.hskn.hss.module.tlemployee.entity.TIEmployeeResult;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlequip.entity.TlEquip;
import com.hskn.hss.module.tlequip.mapper.TlEquipMapper;
import com.hskn.hss.module.tlresplan.entity.TlEmergentResplan;
import com.hskn.hss.module.tlresplan.mapper.TlEmergentResplanMapper;
import com.hskn.hss.module.tlthreshold.mapper.TlThresholdMapper;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.mapper.TlWorkTypeMapper;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.workkaoqin.mapper.WorkKaoqinMapper;
import com.hskn.hss.module.worklog.entity.WorkLog;
import com.hskn.hss.module.worklog.mapper.WorkLogMapper;
import com.hskn.hss.module.workruleitem.mapper.WorkRuleItemMapper;
import com.hskn.hss.utils.*;
import com.hskn.hss.utils.PerformanceMonitor.PerformanceMonitor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-17
 */
@Slf4j
@Service
public class DataBigScreenServiceImpl implements IDataBigScreenService {


    @Resource
    RptCarWorkMapper rptCarWorkMapper;
    @Resource
    TlChannelMapper tlChannelMapper;
    @Resource
    private TlEmployeeMapper tlEmployeeMapper;
    @Resource
    private TlEquipMapper tlEquipMapper;
    @Resource
    private RptEmployeeWorkMapper rptEmployeeWorkMapper;
    @Resource
    private CarTrackMapper carTrackMapper;
    @Resource
    private EmployeeTrackMapper employeeTrackMapper;
    @Resource
    private LocationUtils locationUtil;
    @Resource
    private AlarmCarMapper alarmCarMapper;
    @Resource
    private CarViolationMapper carViolationMapper;
    @Resource
    private AlarmPersonMapper alarmPersonMapper;
    @Resource
    private DlbjViolationMapper dlbjViolationMapper;
    @Resource
    private AlarmSheshiMapper alarmSheshiMapper;
    @Resource
    private SysDepartMapper sysDepartMapper;
    @Resource
    private TlWorkTypeMapper tlWorkTypeMapper;
    @Resource
    private ElementTotalMapper elementTotalMapper;
    @Resource
    private TlEmergentPlanMapper tlEmergentPlanMapper;
    @Resource
    private TlEmergentResplanMapper tlEmergentResplanMapper;
    @Resource
    private CasesMapper casesMapper;
    @Resource
    private TlEquiptypeMapper tlEquiptypeMapper;
    @Resource
    private EmployeePunchCardMapper employeePunchCardMapper;
    @Resource
    private WorkLogMapper workLogMapper;
    @Resource
    private TlCarMapper tlCarMapper;


    @Override
    public JSONObject personMonitor(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        Integer total = 0;
        Integer onlineCount = 0;
        Integer chuqinCount = 0;
        //查询所有人员，以工种分组，获取总人数、在线人数、在岗人数
        JSONObject jp = new JSONObject();
        List<TIEmployeeResult> employeeResults = tlEmployeeMapper.getNumByWorkType(jp);
        //获取总人数、在线总人数、在岗总人数
        for (TIEmployeeResult employeeResult : employeeResults) {
            total += employeeResult.getNum();
            onlineCount += employeeResult.getOnlinum();
            chuqinCount += employeeResult.getChuqinnum();
        }
        BigDecimal onlineRate = BigDecimal.ZERO;
        BigDecimal chuqinRate = BigDecimal.ZERO;
        if (total != 0) {
            onlineRate = (new BigDecimal(onlineCount).divide(new BigDecimal(total), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_UP);

            chuqinRate = (new BigDecimal(chuqinCount).divide(new BigDecimal(total), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_UP);

        }

        json.put("total", total);
        json.put("onlineCount", onlineCount);
        json.put("onlineRate", onlineRate);
        json.put("chuqinCount", chuqinCount);
        json.put("chuqinRate", chuqinRate);
        json.put("worktype", employeeResults);
        return json;
    }

    @Override
    public JSONObject personWorkMonitor(JSONObject params) throws HssException {

        JSONObject json = new JSONObject();
        QueryWrapper<TlEmployee> tlEmployeeQueryWrapper = new QueryWrapper<>();
        tlEmployeeQueryWrapper.lambda().eq(TlEmployee::getShiftStatus, 0);
        List<TlEmployee> tlEmployeeList = tlEmployeeMapper.selectList(tlEmployeeQueryWrapper);

        QueryWrapper<TlEquip> tlEquipQueryWrapper = new QueryWrapper<>();
        List<TlEquip> tlEquipList = tlEquipMapper.selectList(tlEquipQueryWrapper);

        Map<String, List<TlEquip>> tlEquipCollect = tlEquipList.stream().filter(item -> StringUtils.isNotEmpty(item.getBindobjid())).collect(Collectors.groupingBy(TlEquip::getBindobjid));

        QueryWrapper<TlEquiptype> tlEquiptypeQueryWrapper = new QueryWrapper<>();
        List<TlEquiptype> tlEquiptypeList = tlEquiptypeMapper.selectList(tlEquiptypeQueryWrapper);

        Map<String, TlEquiptype> tlEquiptypeMap = tlEquiptypeList.stream().collect(Collectors.groupingBy(TlEquiptype::getId, Collectors.collectingAndThen(Collectors.toList(), tlEquiptypes -> tlEquiptypes.get(0))));

        QueryWrapper<TlChannel> tlChannelQueryWrapper = new QueryWrapper<>();
        tlChannelQueryWrapper.lambda().orderByAsc(TlChannel::getChannelname);
        Map<Object, List<TlChannel>> tlChannelCollect = tlChannelMapper.selectList(tlChannelQueryWrapper).stream().filter(item -> StringUtils.isNotEmpty(item.getEquipid()))
                .collect(Collectors.groupingBy(TlChannel::getEquipid));

        Map<String, TlWorkType> tlWorkTypeCollect = tlWorkTypeMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(TlWorkType::getId, Collectors.collectingAndThen(Collectors.toList(), tlWorkTypes -> tlWorkTypes.get(0))));

        Map<String, SysDepart> sysDepartCollect = sysDepartMapper.selectList(new QueryWrapper<>()).stream().collect(Collectors.groupingBy(SysDepart::getId, Collectors.collectingAndThen(Collectors.toList(), sysDeparts -> sysDeparts.get(0))));

        List<EmpSearchListVo> empSearchListVoList = new ArrayList<>();
        tlEmployeeList.forEach(tlEmployee -> {
            EmpSearchListVo empSearchListVo = new EmpSearchListVo();
            empSearchListVo.setType("人员");
            empSearchListVo.setShiftStatus(tlEmployee.getShiftStatus());
            empSearchListVo.setOnGuardStatus(tlEmployee.getOnguardstatus());
            empSearchListVo.setAttendance(tlEmployee.getAttendance());
            empSearchListVo.setPersonPhoto(tlEmployee.getPersonphoto());
            empSearchListVo.setId(tlEmployee.getId());
            empSearchListVo.setAddr(tlEmployee.getAddr());
            empSearchListVo.setOnlineStatus("2");
            empSearchListVo.setVideoOnlineStatus("2");
            List<TlEquip> tlEquips = tlEquipCollect.get(tlEmployee.getId());
            if (null != tlEquips) {

                List<TlEquip> locationOnlineList = tlEquips.stream().filter(tlEquip -> null != tlEquip.getEquiptypeid() && tlEquip.getEquiptypeid().equals("dc605bf7800b204e3015ac977542e66b")).collect(Collectors.toList());
                locationOnlineList.forEach(tlEquip -> {
                    if (StringUtils.isNotEmpty(tlEquip.getOnlinestatus())) {
                        empSearchListVo.setOnlineStatus(tlEquip.getOnlinestatus());
                    } else {
                        empSearchListVo.setOnlineStatus("2");
                    }
                });
                List<TlEquip> videoOnlineList = tlEquips.stream().filter(tlEquip -> null != tlEquip.getEquiptypeid() && tlEquip.getEquiptypeid().equals("10d4a7cced3e151606c42e7901757b0a")).collect(Collectors.toList());
                videoOnlineList.forEach(tlEquip -> {
                    empSearchListVo.setEquipId(tlEquip.getId());
                    empSearchListVo.setEquipCode(tlEquip.getEquipcode());
                    TlEquiptype tlEquiptype = tlEquiptypeMap.get(tlEquip.getEquiptypeid());
                    if (null != tlEquiptype) {
                        empSearchListVo.setEqpType(tlEquiptype.getEquipname());
                    }
                    if (StringUtils.isNotEmpty(tlEquip.getOnlinestatus())) {
                        empSearchListVo.setVideoOnlineStatus(tlEquip.getOnlinestatus());
                    } else {
                        empSearchListVo.setVideoOnlineStatus("2");
                    }
                    empSearchListVo.setTlChannelList(tlChannelCollect.get(tlEquip.getId()));
                });
            }
            TlWorkType tlWorkType = tlWorkTypeCollect.get(tlEmployee.getWorktype());
            if (null != tlWorkType) {
                empSearchListVo.setTypeName(tlWorkType.getWorktype());
                empSearchListVo.setName(tlEmployee.getName() + "[" + tlWorkType.getWorktype() + "]");
            }
            SysDepart sysDepart = sysDepartCollect.get(tlEmployee.getEmpldeptid());
            if (null != sysDepart) {
                empSearchListVo.setCompany(sysDepart.getDepartName());
            }
            empSearchListVoList.add(empSearchListVo);
        });

        List<EmpSearchListVo> empSelectList = new ArrayList<>();
        empSelectList = empSearchListVoList.stream().filter(empSearchListVo -> (null != empSearchListVo.getTypeName() && empSearchListVo.getTypeName().equals("清洁工"))).collect(Collectors.toList());

        //今日排班计划数
        QueryWrapper<RptEmployeeWork> rptEmployeeWrokWrapper = new QueryWrapper<>();
        String day = DateUtil.formatDate(new Date());
        rptEmployeeWrokWrapper.lambda().eq(RptEmployeeWork::getWorkDate, day);
        List<RptEmployeeWork> rptEmployeeWorks = rptEmployeeWorkMapper.selectList(rptEmployeeWrokWrapper);
        Map<String, RptEmployeeWork> rptEmployeeWorkMap = rptEmployeeWorks.stream().collect(Collectors.groupingBy(RptEmployeeWork::getPlanId, Collectors.collectingAndThen(Collectors.toList(), a -> a.get(0))));

        Map<String, List<EmpSearchListVo>> onlineStatusMap = empSelectList.stream().filter(empSearchListVo -> (CommonConstant.ONLINE_STATUS_YES.equals(empSearchListVo.getOnlineStatus()))).collect(Collectors.groupingBy(EmpSearchListVo::getOnlineStatus));
        Map<Integer, List<EmpSearchListVo>> onGuardStatusMap = empSelectList.stream().filter(empSearchListVo -> (CommonConstant.ONGUARD_STATUS_YES.equals(empSearchListVo.getOnGuardStatus()))).collect(Collectors.groupingBy(EmpSearchListVo::getOnGuardStatus));
        Map<Integer, List<EmpSearchListVo>> attendanceMap = empSelectList.stream().filter(empSearchListVo -> (CommonConstant.ATTENDANCE_YES.equals(empSearchListVo.getAttendance()))).collect(Collectors.groupingBy(EmpSearchListVo::getAttendance));
        json.put("onlineStatusCount", onlineStatusMap.size() > 0 ? onlineStatusMap.get(CommonConstant.ONLINE_STATUS_YES).size() : 0);
        json.put("onGuardStatusCount", onGuardStatusMap.size() > 0 ? onlineStatusMap.get(CommonConstant.ONGUARD_STATUS_YES).size() : 0);
        json.put("attendanceCount", attendanceMap.size() > 0 ? onlineStatusMap.get(CommonConstant.ATTENDANCE_YES).size() : 0);
        json.put("workPlanCount", Objects.nonNull(rptEmployeeWorkMap) ? rptEmployeeWorkMap.size() : 0);
        return json;
    }


    @Override
    public JSONObject carMonitor(JSONObject params) throws HssException {

        Set<String> carIds = new HashSet<>();
        QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<>();
        rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, StringUtils.dateToStr(new Date(), "yyyy-MM-dd"));
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(rptCarWorkQueryWrapper);
        for (RptCarWork rptCarWork : rptCarWorkList) {
            carIds.add(rptCarWork.getCarId());
        }

        JSONObject json = new JSONObject();
        Integer total = 0;
        Integer onlineCount = 0;
        Integer chuQinCount = 0;
        if (CollectionUtils.isEmpty(carIds)) {
            carIds.add("-1");
        }
        //查询所有车辆，以车辆类型分组，获取总数、在线数、在岗数
        List<TICarResult> carResults = tlCarMapper.getNumByCarType(carIds);
        //获取总人数、在线总人数、在岗总人数
        for (TICarResult carResult : carResults) {
            total += carResult.getNum();
            onlineCount += carResult.getOnlinum();
            chuQinCount += carResult.getChuqinnum();
        }
        BigDecimal onlineRate = BigDecimal.ZERO;
        BigDecimal chuqinRate = BigDecimal.ZERO;
        if (total != 0) {
            onlineRate = (new BigDecimal(onlineCount).divide(new BigDecimal(total), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_UP);

            chuqinRate = (new BigDecimal(chuQinCount).divide(new BigDecimal(total), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))).setScale(0, BigDecimal.ROUND_UP);

        }
        json.put("total", total);
        json.put("onlineCount", onlineCount);
        json.put("onlineRate", onlineRate);
        json.put("chuqinCount", chuQinCount);
        json.put("chuqinRate", chuqinRate);
        return json;
    }

    @Override
    public JSONObject jxzyWorkScene(JSONObject params) throws HssException {

        Date date = new Date();
        String now = DateUtil.formatDate(date);
        // 作业类型：2机械清扫；3垃圾清运；4垃圾转运
        // 状态：0未开始；1进行中；2已结束
        Map<Object, Object> jxqsMap = new HashMap<>();
        jxqsMap.put("workType", 2);
        jxqsMap.put("workDate", now);
        List<RptCarWork> jxqsResultList = rptCarWorkMapper.listByPlan(jxqsMap);

        Integer totalCount = jxqsResultList.size();
        Integer waitingCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 0).count();
        Integer workingCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 1).count();
        Integer finishCount = (int) jxqsResultList.stream().filter(t -> t.getAllStatus() == 3).count();
        System.out.println("=====================");
        // 清掃
        int totalCountQs = (int) jxqsResultList.stream().filter(t -> "清扫作业".equals(t.getWorkTypeName())).count();
        int finishCountQs = (int) jxqsResultList.stream().filter(t -> "清扫作业".equals(t.getWorkTypeName())).filter(t1 -> t1.getAllStatus() == 3).count();
        BigDecimal finishRateQs = (finishCountQs == 0) ? BigDecimal.ZERO : (new BigDecimal(finishCountQs).divide(new BigDecimal(totalCountQs), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP));
        // 灑水
        int totalCountSs = (int) jxqsResultList.stream().filter(t -> "洒水作业".equals(t.getWorkTypeName())).count();
        int finishCountSs = (int) jxqsResultList.stream().filter(t -> "洒水作业".equals(t.getWorkTypeName())).filter(t1 -> t1.getAllStatus() == 3).count();
        BigDecimal finishRateSs = (finishCountSs == 0) ? BigDecimal.ZERO : (new BigDecimal(finishCountSs).divide(new BigDecimal(totalCountSs), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP));

        // 汇总
        BigDecimal finishRateTotal = (finishCountQs + finishCountSs == 0) ? BigDecimal.ZERO : (new BigDecimal(finishCountQs + finishCountSs).divide(new BigDecimal(totalCountQs + totalCountSs), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP));


        JSONObject json = new JSONObject();

        json.put("total", totalCount);
        json.put("finished", finishCount);
        json.put("working", workingCount);
        json.put("other", waitingCount);
        json.put("qszyFinishRate", finishRateQs);
        json.put("sszyFinishRate", finishRateSs);
        json.put("totalFinishRate", finishRateTotal);
        return json;
    }

    @Override
    public List<WorkVo> workBroadcast(JSONObject params) throws HssException {

        Date date = new Date();
        String now = DateUtil.formatDate(date);
        String startTime = AttributeUtils.timeCompletion(now, 's');
        String endTime = AttributeUtils.timeCompletion(now, 'e');

        List<WorkVo> resultList = new ArrayList<>();
        //查询今天打卡记录
        QueryWrapper<EmployeePunchCard> employeePunchCardQueryWrapper = new QueryWrapper<>();
        employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getPunchCardDate, now);
        List<EmployeePunchCard> employeePunchCfardList = employeePunchCardMapper.selectList(employeePunchCardQueryWrapper);
        employeePunchCfardList.forEach(t -> {
            WorkVo workVo = new WorkVo();
            workVo.setName(t.getEmployeeName());
            if (StringUtils.isNotEmpty(t.getWorkTime())) {
                workVo.setContent("上班打卡");
                workVo.setTime(DateUtil.parseDateTime(t.getPunchCardDate() + " " + t.getWorkTime()));
                resultList.add(workVo);
            } else if (StringUtils.isNotEmpty(t.getAfterWorkTime())) {
                workVo.setContent("下班打卡");
                workVo.setTime(DateUtil.parseDateTime(t.getPunchCardDate() + " " + t.getAfterWorkTime()));
                resultList.add(workVo);
            }
        });

        //查询车辆今天完成作业记录
        Map<Object, Object> jxqsMap = new HashMap<>();
        jxqsMap.put("workDate", now);
        List<RptCarWork> jxqsResultList = rptCarWorkMapper.listByPlan(jxqsMap);
        jxqsResultList = jxqsResultList.stream().filter(t -> t.getAllStatus() == 3).collect(Collectors.toList());
        jxqsResultList.forEach(t -> {
            WorkVo workVo = new WorkVo();
            workVo.setName(t.getCarNum());
            workVo.setContent("完成" + t.getPlanName() + "作业");
            Date updateTime = Objects.nonNull(t.getWorkEndTime()) ? t.getWorkEndTime() : t.getUpdateTime();
            workVo.setTime(Objects.nonNull(updateTime) ? updateTime : t.getCreateTime());
            resultList.add(workVo);
        });

        //查询人员今天完成作业记录
        QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<>();
        rptEmployeeWorkQueryWrapper.lambda().between(RptEmployeeWork::getUpdateTime, startTime, endTime);
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(rptEmployeeWorkQueryWrapper);
        rptEmployeeWorkList = rptEmployeeWorkList.stream().filter(t -> t.getStatus() == 2).collect(Collectors.toList());
        rptEmployeeWorkList.forEach(t -> {
            WorkVo workVo = new WorkVo();
            workVo.setName(t.getEmployeeName());
            workVo.setContent("完成" + t.getPlanName() + "作业");
            Date updateTime = Objects.nonNull(t.getWorkEndTime()) ? t.getWorkEndTime() : t.getUpdateTime();
            workVo.setTime(Objects.nonNull(updateTime) ? updateTime : t.getCreateTime());
            resultList.add(workVo);
        });

        //查询今天提报的工作日志
        QueryWrapper<WorkLog> workLogQueryWrapper = new QueryWrapper<>();
        workLogQueryWrapper.lambda().between(WorkLog::getCreateTime, startTime, endTime);
        List<WorkLog> workLogList = workLogMapper.selectList(workLogQueryWrapper);
        workLogList.forEach(t -> {
            WorkVo workVo = new WorkVo();
            workVo.setName(t.getCreateName());
            workVo.setContent("提交" + t.getTitle() + "作业日志");
            workVo.setTime(t.getCreateTime());
            resultList.add(workVo);
        });

        Collections.sort(resultList, (o1, o2) -> {
            Date d1 = o1.getTime();
            Date d2 = o2.getTime();
            return d2.compareTo(d1);
        });

        return resultList;
    }


    @Override
    public JSONObject facilitiesMonitor(JSONObject params) throws HssException {

        List<Map> toiletList = elementTotalMapper.getToiletNumMap(params);
        //设施数量
        Integer facilitiesNum = 0;
        if (Objects.nonNull(toiletList) && toiletList.size() > 0) {
            facilitiesNum = Integer.valueOf(String.valueOf(toiletList.get(0).get("num")));
        }

        List<Map> toiletDetailList = elementTotalMapper.getToiletDetailMap(params);
        //设施当日人流总量
        Integer pedestrianVolume = 0;
        if (Objects.nonNull(toiletDetailList) && toiletDetailList.size() > 0) {
            for (Map map : toiletDetailList
            ) {
                pedestrianVolume += Integer.valueOf(String.valueOf(map.get("people")));
            }
        }

        params.put("dateType", 1);
        List<Map> toiletDetailMonthList = elementTotalMapper.getToiletDetailMap(params);
        //设施当日人流总量
        Integer pedestrianVolumeMonth = 0;
        if (Objects.nonNull(toiletDetailMonthList) && toiletDetailMonthList.size() > 0) {
            for (Map map : toiletDetailMonthList
            ) {
                pedestrianVolumeMonth += Integer.valueOf(String.valueOf(map.get("people")));
            }
        }
        JSONObject json = new JSONObject();

        json.put("facilitiesNum", facilitiesNum);
        json.put("pedestrianVolume", pedestrianVolume);
        json.put("pedestrianVolumeMonth", pedestrianVolumeMonth);
        return json;
    }

    @Override
    public JSONObject mileageMonitor(JSONObject params) throws HssException {

        String now = DateUtil.formatDate(new Date());
        // 作业类型：2机械清扫；3垃圾清运；4垃圾转运
        QueryWrapper<RptCarWork> carWorkQueryWrapper = new QueryWrapper<>();
        carWorkQueryWrapper.lambda().eq(RptCarWork::getWorkDate, now);
        List<RptCarWork> rptCarWorkList = rptCarWorkMapper.selectList(carWorkQueryWrapper);
        List<String> carIds = new ArrayList<>();
        for (RptCarWork rptCarWork : rptCarWorkList) {
            carIds.add(rptCarWork.getCarId());
        }
        JSONObject json = new JSONObject();

        BigDecimal carMileageSum = BigDecimal.ZERO;
        if (!carIds.isEmpty()) {
            QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
            carTrackQueryWrapper.lambda().orderByAsc(CarTrack::getTimestamp);
            carTrackQueryWrapper.lambda().eq(CarTrack::getWorkDay, LocalDate.now());
            carTrackQueryWrapper.lambda().in(CarTrack::getCarId, carIds);
            List<CarTrack> carTrackList = carTrackMapper.selectList(carTrackQueryWrapper);
            Map<String, List<CarTrack>> map = carTrackList.stream().collect(Collectors.groupingBy(CarTrack::getCarId));
            for (String carId : map.keySet()) {
                for (int i = 0; i < map.get(carId).size(); i++) {
                    if (i > 0) {
                        carMileageSum = MathUtils.getBigDecimal(locationUtil.getDistance(map.get(carId).get(i - 1).getLat(), map.get(carId).get(i - 1).getLng(), map.get(carId).get(i).getLat(), map.get(carId).get(i).getLng())).add(carMileageSum);
                    }
                }
            }
        }

        QueryWrapper<RptEmployeeWork> employeeWorkQueryWrapper = new QueryWrapper<>();
        employeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, now);
        List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkMapper.selectList(employeeWorkQueryWrapper);
        List<String> employeeIds = new ArrayList<>();
        for (RptEmployeeWork rptEmployeeWork : rptEmployeeWorkList) {
            employeeIds.add(rptEmployeeWork.getEmployeeId());
        }

        BigDecimal employeeMileageSum = BigDecimal.ZERO;
        if (!employeeIds.isEmpty()) {
            QueryWrapper<EmployeeTrack> employeeTrackQueryWrapper = new QueryWrapper<>();
            employeeTrackQueryWrapper.lambda().orderByAsc(EmployeeTrack::getTimestamp);
            employeeTrackQueryWrapper.lambda().eq(EmployeeTrack::getWorkDay, LocalDate.now());
            employeeTrackQueryWrapper.lambda().in(EmployeeTrack::getEmployeeId, employeeIds);
            List<EmployeeTrack> carTrackList = employeeTrackMapper.selectList(employeeTrackQueryWrapper);
            Map<String, List<EmployeeTrack>> map = carTrackList.stream().collect(Collectors.groupingBy(EmployeeTrack::getEmployeeId));
            for (String employeeId : map.keySet()) {
                for (int i = 0; i < map.get(employeeId).size(); i++) {
                    if (i > 0) {
                        employeeMileageSum = MathUtils.getBigDecimal(locationUtil.getDistance(map.get(employeeId).get(i - 1).getLat(), map.get(employeeId).get(i - 1).getLng(), map.get(employeeId).get(i).getLat(), map.get(employeeId).get(i).getLng())).add(employeeMileageSum);
                    }
                }
            }
        }

        json.put("carMileageSum", carMileageSum.divide(new BigDecimal(1000), 0, BigDecimal.ROUND_HALF_UP));
        json.put("employeeMileageSum", employeeMileageSum.divide(new BigDecimal(1000), 0, BigDecimal.ROUND_HALF_UP));
        return json;
    }

    @Override
    public JSONObject abnormalMonitor(JSONObject params) throws HssException {

        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');
        String endTime = AttributeUtils.timeCompletion(day, 'e');
        //当日车辆报警总数
        Integer alarmCarNum = 0;
        //当日车辆违规总数
        Integer carViolationNum = 0;
        //当日设施报警总数
        Integer alarmSheshiNum = 0;
        //当日人员报警总数
        Integer alarmPersonNum = 0;
        //当日人员违规总数
        Integer dlbjViolationNum = 0;

        //当日车辆报警
        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, startTime, endTime);
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        if (Objects.nonNull(alarmCarList) && alarmCarList.size() > 0) {
            alarmCarNum = alarmCarList.size();
        }

        //当日车辆违规
        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().between(CarViolation::getTime, startTime, endTime);
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        if (Objects.nonNull(carViolationList) && carViolationList.size() > 0) {
            carViolationNum = carViolationList.size();
        }

        //当日设施报警
        QueryWrapper<AlarmSheshi> alarmSheshiQueryWrapper = new QueryWrapper<>();
        alarmSheshiQueryWrapper.lambda().between(AlarmSheshi::getAlarmTime, startTime, endTime);
        List<AlarmSheshi> alarmSheshiList = alarmSheshiMapper.selectList(alarmSheshiQueryWrapper);
        if (Objects.nonNull(alarmSheshiList) && alarmSheshiList.size() > 0) {
            alarmSheshiNum = alarmSheshiList.size();
        }

        //当日人员报警
        QueryWrapper<AlarmPerson> alarmPersonQueryWrapper = new QueryWrapper<>();
        alarmPersonQueryWrapper.lambda().between(AlarmPerson::getAlarmTime, startTime, endTime);
        List<AlarmPerson> alarmPersonList = alarmPersonMapper.selectList(alarmPersonQueryWrapper);
        if (Objects.nonNull(alarmPersonList) && alarmPersonList.size() > 0) {
            alarmPersonNum = alarmPersonList.size();
        }

        //当日人员违规
        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        dlbjViolationQueryWrapper.lambda().between(DlbjViolation::getDate, startTime, endTime);
        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);
        if (Objects.nonNull(dlbjViolationList) && dlbjViolationList.size() > 0) {
            dlbjViolationNum = dlbjViolationList.size();
        }

        JSONObject json = new JSONObject();
        json.put("alarmCarNum", alarmCarNum);
        json.put("carViolationNum", carViolationNum);
        json.put("alarmSheshiNum", alarmSheshiNum);
        json.put("alarmPersonNum", alarmPersonNum);
        json.put("dlbjViolationNum", dlbjViolationNum);
        return json;
    }

    @Override
    public List<AbnormalVo> abnormalBroadcast(JSONObject params) throws HssException {

        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');
        String endTime = AttributeUtils.timeCompletion(day, 'e');

        List<AbnormalVo> dataBigScreenAbnormalResultList = new ArrayList<>();
        //当日车辆报警
        QueryWrapper<AlarmCar> alarmCarQueryWrapper = new QueryWrapper<>();
        alarmCarQueryWrapper.lambda().between(AlarmCar::getAlarmTime, startTime, endTime);
        alarmCarQueryWrapper.lambda().last(" LIMIT 20");
        List<AlarmCar> alarmCarList = alarmCarMapper.selectList(alarmCarQueryWrapper);
        for (AlarmCar alarmCar : alarmCarList
        ) {
            AbnormalVo dataBigScreenAbnormalResult = new AbnormalVo();
            dataBigScreenAbnormalResult.setId(alarmCar.getAlarmCarId());
            dataBigScreenAbnormalResult.setMessage(alarmCar.getCarName() + alarmCar.getContent());
            dataBigScreenAbnormalResult.setValue(alarmCar.getAlarmValue());
            dataBigScreenAbnormalResult.setTime(alarmCar.getAlarmTime());
            dataBigScreenAbnormalResultList.add(dataBigScreenAbnormalResult);
        }


        //当日车辆违规
        QueryWrapper<CarViolation> carViolationQueryWrapper = new QueryWrapper<>();
        carViolationQueryWrapper.lambda().between(CarViolation::getTime, startTime, endTime);
        carViolationQueryWrapper.lambda().last(" LIMIT 20");
        List<CarViolation> carViolationList = carViolationMapper.selectList(carViolationQueryWrapper);
        for (CarViolation carViolation : carViolationList
        ) {
            AbnormalVo dataBigScreenAbnormalResult = new AbnormalVo();
            dataBigScreenAbnormalResult.setId(carViolation.getCarViolationId());
            dataBigScreenAbnormalResult.setMessage(carViolation.getCarName() + carViolation.getContent());
            dataBigScreenAbnormalResult.setValue(carViolation.getViolationValue());
            dataBigScreenAbnormalResult.setTime(carViolation.getTime());
            dataBigScreenAbnormalResultList.add(dataBigScreenAbnormalResult);
        }

        //当日设施报警
        QueryWrapper<AlarmSheshi> alarmSheshiQueryWrapper = new QueryWrapper<>();
        alarmSheshiQueryWrapper.lambda().between(AlarmSheshi::getAlarmTime, startTime, endTime);
        alarmSheshiQueryWrapper.lambda().last(" LIMIT 20");
        List<AlarmSheshi> alarmSheshiList = alarmSheshiMapper.selectList(alarmSheshiQueryWrapper);
        for (AlarmSheshi alarmSheshi : alarmSheshiList
        ) {
            AbnormalVo dataBigScreenAbnormalResult = new AbnormalVo();
            dataBigScreenAbnormalResult.setId(alarmSheshi.getAlarmSheshiId());
            dataBigScreenAbnormalResult.setMessage(alarmSheshi.getSheshiName() + alarmSheshi.getContent());
            dataBigScreenAbnormalResult.setValue(alarmSheshi.getAlarmValue());
            dataBigScreenAbnormalResult.setTime(alarmSheshi.getAlarmTime());
            dataBigScreenAbnormalResultList.add(dataBigScreenAbnormalResult);
        }

        //当日人员报警
        QueryWrapper<AlarmPerson> alarmPersonQueryWrapper = new QueryWrapper<>();
        alarmPersonQueryWrapper.lambda().between(AlarmPerson::getAlarmTime, startTime, endTime);
        alarmPersonQueryWrapper.lambda().last(" LIMIT 20");
        List<AlarmPerson> alarmPersonList = alarmPersonMapper.selectList(alarmPersonQueryWrapper);
        for (AlarmPerson alarmPerson : alarmPersonList
        ) {
            AbnormalVo dataBigScreenAbnormalResult = new AbnormalVo();
            dataBigScreenAbnormalResult.setId(alarmPerson.getAlarmPersonId());
            dataBigScreenAbnormalResult.setMessage(alarmPerson.getPersonName() + alarmPerson.getContent());
            dataBigScreenAbnormalResult.setValue(new BigDecimal(String.valueOf(StringUtils.calLastedTime(alarmPerson.getAlarmStartTime(), alarmPerson.getAlarmEndTime()))));
            dataBigScreenAbnormalResult.setTime(alarmPerson.getAlarmTime());
            dataBigScreenAbnormalResultList.add(dataBigScreenAbnormalResult);
        }

        //当日人员违规
        QueryWrapper<DlbjViolation> dlbjViolationQueryWrapper = new QueryWrapper<>();
        dlbjViolationQueryWrapper.lambda().between(DlbjViolation::getDate, startTime, endTime);
        dlbjViolationQueryWrapper.lambda().last(" LIMIT 20");
        List<DlbjViolation> dlbjViolationList = dlbjViolationMapper.selectList(dlbjViolationQueryWrapper);
        for (DlbjViolation dlbjViolation : dlbjViolationList
        ) {
            AbnormalVo dataBigScreenAbnormalResult = new AbnormalVo();
            dataBigScreenAbnormalResult.setId(dlbjViolation.getDlbjViolationId());
            dataBigScreenAbnormalResult.setMessage(dlbjViolation.getPersonName() + dlbjViolation.getContent());
            dataBigScreenAbnormalResult.setValue(dlbjViolation.getViolationValue());
            dataBigScreenAbnormalResult.setTime(dlbjViolation.getDate());
            dataBigScreenAbnormalResultList.add(dataBigScreenAbnormalResult);
        }

        //按照设备上传定位时间倒序排序
        Collections.sort(dataBigScreenAbnormalResultList, (o1, o2) -> {
            Date d1 = o1.getTime();
            Date d2 = o2.getTime();
            return d2.compareTo(d1);
        });

        return dataBigScreenAbnormalResultList;
    }

    @Override
    public JSONObject emergentMonitor(JSONObject params) {

        Integer planCount = 0;
        Integer resplanCount = 0;

        // 计划的
        QueryWrapper<TlEmergentPlan> tlEmergentPlanWrapper = new QueryWrapper<>();
        List<TlEmergentPlan> tlEmergentPlanList = tlEmergentPlanMapper.selectList(tlEmergentPlanWrapper);
        if (Objects.nonNull(tlEmergentPlanList) && tlEmergentPlanList.size() > 0) {
            planCount = tlEmergentPlanList.size();
        }

        //预案
        QueryWrapper<TlEmergentResplan> tlEmergentResplanWrapper = new QueryWrapper<>();
        List<TlEmergentResplan> tlEmergentResplanList = tlEmergentResplanMapper.selectList(tlEmergentResplanWrapper);
        if (Objects.nonNull(tlEmergentResplanList) && tlEmergentResplanList.size() > 0) {
            resplanCount = tlEmergentResplanList.size();
        }

        JSONObject json = new JSONObject();
        json.put("planCount", planCount);
        json.put("resplanCount", resplanCount);
        return json;
    }

    @Override
    public CasesVo patrolCasesMonitor(JSONObject params) {

        CasesVo casesVo = new CasesVo();
        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');
        String endTime = AttributeUtils.timeCompletion(day, 'e');
        String startTimeMonth = AttributeUtils.timeCompletion(StringUtils.firstDayByMonth(), 's');
        String endTimeMonth = AttributeUtils.timeCompletion(StringUtils.lastDayByMonth(), 'e');
        // 当日的
        QueryWrapper<Cases> casesQueryWrapper = new QueryWrapper<>();
        casesQueryWrapper.lambda().between(Cases::getCreateTime, startTime, endTime);
        List<Cases> casesList = casesMapper.selectList(casesQueryWrapper);
        if (Objects.nonNull(casesList) && casesList.size() > 0) {
            casesVo.setCaseSumToday(casesList.size());
            Map<Integer, List<Cases>> collect = casesList.stream().collect(Collectors.groupingBy(Cases::getStatus));
            collect.keySet().forEach(t -> {
                List<Cases> cases = collect.get(t);
                if (2 == t) {
                    casesVo.setPendingToday(cases.size());
                }
                if (3 == t || 4 == t) {
                    casesVo.setHandledToday(casesVo.getHandledToday() + cases.size());
                }
                if (4 == t) {
                    casesVo.setClosedCaseToday(casesVo.getClosedCaseToday() + cases.size());
                }
            });
        }

        //当月的
        QueryWrapper<Cases> casesQueryWrapper2 = new QueryWrapper<>();
        casesQueryWrapper.lambda().between(Cases::getCreateTime, startTimeMonth, endTimeMonth);
        List<Cases> casesList2 = casesMapper.selectList(casesQueryWrapper2);
        if (Objects.nonNull(casesList2) && casesList2.size() > 0) {
            casesVo.setCaseSumMonth(casesList2.size());
            Map<Integer, List<Cases>> collect = casesList2.stream().collect(Collectors.groupingBy(Cases::getStatus));
            collect.keySet().forEach(t -> {
                List<Cases> cases = collect.get(t);
                if (2 == t) {
                    casesVo.setPendingMonth(cases.size());
                }
                if (3 == t || 4 == t) {
                    casesVo.setHandledMonth(casesVo.getHandledToday() + cases.size());
                }
                if (4 == t) {
                    casesVo.setClosedCaseMonth(casesVo.getClosedCaseToday() + cases.size());
                }
            });
        }

        return casesVo;
    }

    @Override
    public List<Cases> caseBroadcast(JSONObject params) {

        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');
        String endTime = AttributeUtils.timeCompletion(day, 'e');
        // 当日的
        QueryWrapper<Cases> casesQueryWrapper = new QueryWrapper<>();
        casesQueryWrapper.lambda().between(Cases::getCreateTime, startTime, endTime);
        List<Cases> casesList = casesMapper.selectList(casesQueryWrapper);

        return casesList;
    }

    @Override
    public List<Map> emergentPlanTimeTrend(JSONObject params) {

        String uid = LoginUtils.getUser().getUid();
        String yearFirstDay = DateUtil.formatDateTime(StringUtils.getYearFirstDay());

        QueryWrapper<TlEmergentPlan> tlEmergentPlanWrapper = new QueryWrapper<>();
        tlEmergentPlanWrapper.lambda().ge(TlEmergentPlan::getCreatetime, yearFirstDay);
        List<TlEmergentPlan> tlEmergentPlanList = tlEmergentPlanMapper.selectList(tlEmergentPlanWrapper);
        tlEmergentPlanList.forEach(t -> {
            t.setMonth(DateUtil.format(t.getCreatetime(), "yyyy-MM"));
        });

        Map<String, Long> collect = tlEmergentPlanList.stream().collect(Collectors.groupingBy(TlEmergentPlan::getMonth, Collectors.counting()));

        List<Map> resultMap = new ArrayList<>();

        List<String> monthList = Tools.getMonthList(yearFirstDay, DateUtil.formatDateTime(new Date()));
        for (int i = 0; i < monthList.size(); i++) {
            Map map = new HashMap();
            map.put("month", monthList.get(i));
            map.put("count", 0);

            Long aLong = collect.get(monthList.get(i));
            if (Objects.nonNull(aLong)) {
                map.put("count", aLong);
            }
            resultMap.add(map);
        }

        resultMap.forEach(t -> {
            int month = Integer.parseInt(String.valueOf(t.get("month")).substring(5));
            t.put("month", month + "月");
        });
        return resultMap;
    }

    @Override
    public List<Map> emergentBroadcast(JSONObject params) {

        String uid = LoginUtils.getUser().getUid();
        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');

        QueryWrapper<TlEmergentPlan> tlEmergentPlanWrapper = new QueryWrapper<>();
        tlEmergentPlanWrapper.lambda().ge(TlEmergentPlan::getCreatetime, startTime);
        tlEmergentPlanWrapper.lambda().orderByDesc(TlEmergentPlan::getCreatetime);
        List<TlEmergentPlan> tlEmergentPlanList = tlEmergentPlanMapper.selectList(tlEmergentPlanWrapper);

        List<Map> resultList = new ArrayList<>();
        tlEmergentPlanList.forEach(t -> {
            Map map = new HashMap();
            map.put("name", t.getPlanname());
            map.put("content", t.getDetail());
            map.put("time", t.getCreatetime());
            //0 新增 1修改 2 已签到 3已处理 4结案
            if ("0".equals(t.getStatus()) || "1".equals(t.getStatus())) {
                map.put("statuName", "已下发");
            }
            if ("2".equals(t.getStatus())) {
                map.put("statuName", "已签到");
            }
            if ("3".equals(t.getStatus())) {
                map.put("statuName", "已处理");
            }
            if ("4".equals(t.getStatus()) || "1".equals(t.getStatus())) {
                map.put("statuName", "结案");
            }
            resultList.add(map);
        });

        return resultList;
    }

    @Override
    public List<List<String>> heatMap(JSONObject params) {

        String uid = LoginUtils.getUser().getUid();
        String day = DateUtil.formatDate(new Date());
        String startTime = AttributeUtils.timeCompletion(day, 's');

        List<TrackVo> resultList = new ArrayList<>();
        PerformanceMonitor.begin("-----------------------查询人员轨迹表数据执行时间------------------~");
        //人员轨迹表
        QueryWrapper<EmployeeTrack> employeeTrackQueryWrapper = new QueryWrapper<>();
        employeeTrackQueryWrapper.lambda().ge(EmployeeTrack::getTimestamp, startTime);
        List<EmployeeTrack> employeeTracks = employeeTrackMapper.selectList(employeeTrackQueryWrapper);
        employeeTracks.forEach(t -> {
            t.setEmployeeIdAndTime(t.getEmployeeId() + DateUtil.format(t.getTimestamp(), "yyyy-MM-dd HH:mm"));
        });
        Map<String, List<EmployeeTrack>> employeeMap = employeeTracks.stream().collect(Collectors.groupingBy(EmployeeTrack::getEmployeeIdAndTime));
        employeeMap.keySet().forEach(t -> {
            TrackVo trackVo = new TrackVo();
            trackVo.setId(employeeMap.get(t).get(0).getId());
            trackVo.setItemId(employeeMap.get(t).get(0).getEmployeeId());
            String lat = employeeMap.get(t).get(0).getLat();
            String lng = employeeMap.get(t).get(0).getLng();
            trackVo.setLat(lat);
            trackVo.setLng(lng);
            trackVo.setLocation(lng.substring(0, lng.length() - 2) + "," + lat.substring(0, lat.length() - 2));
            resultList.add(trackVo);
        });
        PerformanceMonitor.end();
        PerformanceMonitor.begin("-----------------------查询车辆轨迹表数据执行时间------------------~");
        //车辆轨迹
        QueryWrapper<CarTrack> carTrackQueryWrapper = new QueryWrapper<>();
        carTrackQueryWrapper.lambda().ge(CarTrack::getTimestamp, startTime);
        List<CarTrack> carTracks = carTrackMapper.selectList(carTrackQueryWrapper);
        carTracks.forEach(t -> {
            t.setCarIdAndTime(t.getCarId() + DateUtil.format(t.getTimestamp(), "yyyy-MM-dd HH:mm"));
        });
        Map<String, List<CarTrack>> carMap = carTracks.stream().collect(Collectors.groupingBy(CarTrack::getCarIdAndTime));
        carMap.keySet().forEach(t -> {
            TrackVo trackVo = new TrackVo();
            trackVo.setId(carMap.get(t).get(0).getId());
            trackVo.setItemId(carMap.get(t).get(0).getCarId());
            String lat = carMap.get(t).get(0).getLat();
            String lng = carMap.get(t).get(0).getLng();
            trackVo.setLat(lat);
            trackVo.setLng(lng);
            trackVo.setLocation(lng.substring(0, lng.length() - 2) + "," + lat.substring(0, lat.length() - 2));
            resultList.add(trackVo);
        });
        PerformanceMonitor.end();
        PerformanceMonitor.begin("-----------------------热力图格式化数据执行时间------------------~");
        List<List<String>> list = new ArrayList<>();
        Map<String, Long> collect = resultList.stream().collect(Collectors.groupingBy(TrackVo::getLocation, Collectors.counting()));
        collect.keySet().forEach(t -> {
            List<String> stringList = new ArrayList<>();
            if (StringUtils.isNotEmpty(t)) {
                stringList.add(t.split(",")[0]);
                stringList.add(t.split(",")[1]);
            } else {
                stringList.add("");
                stringList.add("");
            }
            stringList.add(collect.get(t).toString());
            list.add(stringList);
        });
        PerformanceMonitor.end();
        return list;
    }

}
